package cn.edu.buu.hospital.service.impl; 
 
 import cn.edu.buu.hospital.entity.Appointment; 
 import cn.edu.buu.hospital.entity.Pay; 
 import cn.edu.buu.hospital.mapper.AppointmentMapper; 
 import cn.edu.buu.hospital.mapper.VisitMapper; 
 import cn.edu.buu.hospital.service.AppointmentService; 
 import cn.edu.buu.hospital.service.PayService; 
 import com.github.pagehelper.PageHelper; 
 import com.github.pagehelper.PageInfo; 
 import org.springframework.beans.factory.annotation.Autowired; 
 import org.springframework.stereotype.Service; 
 import org.springframework.transaction.annotation.Transactional; 
 
 import java.math.BigDecimal; 
 import java.time.LocalDateTime; 
 import java.util.List; 
 
 /**
  * 预约服务实现类
  * 实现AppointmentService接口，提供预约相关的业务逻辑处理
  * 使用@Service注解标记为Spring服务组件，@Transactional注解开启事务管理
  */
 @Service 
 @Transactional 
 public class AppointmentServiceImpl implements AppointmentService { 
     
     // 注入预约Mapper，用于数据库操作
     @Autowired 
     private AppointmentMapper appointmentMapper; 
     
     // 注入排班Mapper，用于处理与排班相关的操作
     @Autowired 
     private VisitMapper visitMapper; 
     
     // 注入支付服务，用于处理支付相关业务
     @Autowired 
     private PayService payService; 
 
     /**
      * 添加新的预约记录
      * @param appointment 预约实体对象，包含预约相关信息
      */
     @Override 
     public void add(Appointment appointment) { 
         appointmentMapper.insert(appointment); 
     } 
 
     /**
      * 根据预约ID删除预约记录
      * @param appointmentId 预约ID
      */
     @Override 
     public void removeById(Long appointmentId) { 
         appointmentMapper.deleteById(appointmentId); 
     } 
 
     /**
      * 修改预约信息
      * @param appointment 包含更新信息的预约实体对象
      */
     @Override 
     public void modify(Appointment appointment) { 
         appointmentMapper.update(appointment); 
     } 
 
     /**
      * 根据ID查询预约信息
      * @param appointmentId 预约ID
      * @return 预约实体对象，如果不存在则返回null
      */
     @Override 
     public Appointment findById(Long appointmentId) { 
         return appointmentMapper.selectById(appointmentId); 
     } 
 
     /**
      * 查询所有预约记录
      * @return 预约列表
      */
     @Override 
     public List<Appointment> findAll() { 
         return appointmentMapper.selectAll(); 
     } 
 
     /**
      * 分页查询预约记录
      * @param pageNum 当前页码
      * @param pageSize 每页记录数
      * @param condition 查询条件
      * @return 分页查询结果，包含预约列表和分页信息
      */
     @Override 
     public PageInfo<Appointment> find(int pageNum, int pageSize, Appointment condition) { 
         PageHelper.startPage(pageNum, pageSize); 
         List<Appointment> appointments = appointmentMapper.selectByCondition(condition, null); 
         return new PageInfo<>(appointments); 
     } 
     
     /**
      * 根据用户ID分页查询预约记录
      * @param pageNum 当前页码
      * @param pageSize 每页记录数
      * @param userId 用户ID
      * @param condition 查询条件
      * @return 分页查询结果，包含该用户的预约列表和分页信息
      */
     @Override 
     public PageInfo<Appointment> findByUserId(int pageNum, int pageSize, Long userId, Appointment condition) { 
         PageHelper.startPage(pageNum, pageSize); 
         List<Appointment> appointmentList = appointmentMapper.selectByUserId(userId); 
         return new PageInfo<>(appointmentList); 
     } 
 
     /**
      * 根据排班ID查询预约记录
      * @param visitId 排班ID
      * @return 该排班下的所有预约记录列表
      */
     @Override 
     public List<Appointment> findByVisitId(Long visitId) { 
         return appointmentMapper.selectByVisitId(visitId); 
     } 
 
     /**
      * 确认预约
      * 更新预约状态为已完成，并创建对应的支付记录
      * @param appointmentId 预约ID
      */
     @Override 
     public void confirmAppointment(Long appointmentId) { 
         // 更新预约状态为已完成(1)
         appointmentMapper.updateStatus(appointmentId, 1); 
         
         // 创建对应的支付记录
         Pay pay = new Pay(); 
         pay.setAppointmentid(appointmentId); 
         // 设置默认金额（实际应根据业务规则确定）
         pay.setAmount(BigDecimal.valueOf(100.00)); 
         // 设置支付状态为未支付(0)
         pay.setPaystatus(0); 
         // 设置创建时间为当前时间
         pay.setCreatetime(LocalDateTime.now()); 
         
         // 保存支付记录
         payService.add(pay); 
     } 
 
     /**
      * 取消预约
      * 更新预约状态为已取消，并减少对应排班的已预约数量
      * @param appointmentId 预约ID
      */
     @Override 
     public void cancelAppointment(Long appointmentId) { 
         // 先查询该预约对应的排班ID
         Appointment appointment = appointmentMapper.selectById(appointmentId); 
         if (appointment != null && appointment.getVisitid() != null) { 
             // 更新预约状态为已取消(2)
             appointmentMapper.updateStatus(appointmentId, 2); 
             // 减少对应排班的已预约数量
             visitMapper.decreaseUsedNum(appointment.getVisitid()); 
         } 
     } 
 
     /**
      * 统计今天的预约数量
      * @return 今天的预约总数
      */
     @Override 
     public long countTodayAppointments() { 
         return appointmentMapper.countTodayAppointments(); 
     } 
     
     /**
      * 根据预约编号查询预约信息
      * @param appointmentNo 预约编号
      * @return 预约实体对象，如果不存在则返回null
      */
     @Override 
     public Appointment findByNumber(String appointmentNo) { 
         Appointment condition = new Appointment(); 
         condition.setNumber(appointmentNo); 
         List<Appointment> appointments = appointmentMapper.selectByCondition(condition, null); 
         if (appointments != null && !appointments.isEmpty()) { 
             return appointments.get(0); 
         } 
         return null; 
     } 
     
     /**
      * 根据用户ID查询所有预约记录
      * @param userId 用户ID
      * @return 该用户的所有预约记录列表
      */
     @Override 
     public List<Appointment> findByUserId(Long userId) { 
         return appointmentMapper.selectByUserId(userId); 
     } 
 }